package com.yenlien.traceyun.breed.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.breed.dao.BreedCropDao;
import com.yenlien.traceyun.breed.dao.BreedCropDetailDao;
import com.yenlien.traceyun.breed.dao.BreedDetailDao;
import com.yenlien.traceyun.breed.entity.BreedCrop;
import com.yenlien.traceyun.breed.entity.BreedCropDetail;
import com.yenlien.traceyun.breed.entity.BreedDetail;
import com.yenlien.traceyun.common.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/9/23.
 */
@Service
public class BreedCropService {
    @Autowired
    private BreedCropDao breedCropDao;
    @Autowired
    private BreedCropDetailDao breedCropDetailDao;
    @Autowired
    private BreedDetailDao breedDetailDao;

    public Page<BreedCrop> findByList(final BreedCrop breedCrop) {
        Pageable pageable = PageRequest.of(breedCrop.getPage(), breedCrop.getPageSize(), Sort.Direction.DESC, "createtime");
        return breedCropDao.findAll(new Specification<BreedCrop>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<BreedCrop> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> companyId = root.get("companyId");
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                if (!StringUtils.isEmpty(breedCrop.getCompanyId())) {
                    list.add(cb.equal(companyId, breedCrop.getCompanyId()));
                }
                if (!StringUtils.isEmpty(breedCrop.getBatch())) {
                    list.add(cb.like(root.get("batch"), "%" + breedCrop.getBatch() + "%"));
                }
                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        }, pageable);
    }

    public Page<BreedCropDetail> findByDetailList(final BreedCropDetail breedCropDetail) {
        Pageable pageable = PageRequest.of(breedCropDetail.getPage(), breedCropDetail.getPageSize(), Sort.Direction.DESC, "createTime");
        return breedCropDetailDao.findAll(new Specification<BreedCropDetail>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<BreedCropDetail> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //构造查询条件
                List<Predicate> list_and = new ArrayList<>();
                List<Predicate> list_or = new ArrayList<>();
                if (!StringUtils.isEmpty(breedCropDetail.getBreedCropId())) {
                    list_and.add(cb.equal(root.get("breedCropId"), breedCropDetail.getBreedCropId()));
                }
                Predicate[] predicate = new Predicate[list_and.size()];
                Predicate pre_and = cb.and(list_and.toArray(predicate));
                if (!StringUtils.isEmpty(breedCropDetail.getQuery())) {
                    list_or.add(cb.like(root.get("seedName"), "%" + breedCropDetail.getQuery() + "%"));
                    list_or.add(cb.like(root.get("batch"), "%" + breedCropDetail.getQuery() + "%"));
                    list_or.add(cb.like(root.get("code"), "%" + breedCropDetail.getQuery() + "%"));
                } else {
                    return pre_and;
                }
                Predicate[] or = new Predicate[list_or.size()];
                Predicate pre_or = cb.or(list_or.toArray(or));
                return query.where(pre_and, pre_or).getRestriction();
            }
        }, pageable);
    }

    public List<BreedDetail> getBreedDetail(JSONObject jsonObject) {
        List<BreedDetail> objects = new ArrayList<>();
        String type = jsonObject.getString("type");
        JSONArray param = jsonObject.getJSONArray("param");
        if (param.size() > 0) {
            for (int i = 0; i < param.size(); i++) {
                if (type.equals("养殖批号")) {
                    objects.addAll(breedDetailDao.findByBatch(param.get(i).toString()));
                } else {
                    objects.addAll(breedDetailDao.findByTypeAndCode(type, param.get(i).toString()));
                }
            }
        }
        return objects;
    }

    @Transactional
    public void save(BreedCrop breedCrop) {
        breedCropDao.save(breedCrop);
        if (breedCrop.getBreedCropDetails().size() > 0) {
            for (BreedCropDetail breedCropDetail : breedCrop.getBreedCropDetails()) {
                breedCropDetail.setBreedCropId(breedCrop.getId());
                if (StringUtils.isEmpty(breedCropDetail.getCreateTime())) {
                    breedCropDetail.setCreateTime(Tools.getCurrentTime());
                }
            }
            breedCropDetailDao.saveAll(breedCrop.getBreedCropDetails());
        }
    }

    public BreedCrop findById(String id) {
        BreedCrop breedCrop = breedCropDao.findById(id);
        return breedCrop;
    }

    @Transactional
    public void delete(String id) {
        breedCropDao.deleteById(id);
        List<BreedCropDetail> details = breedCropDetailDao.findByBreedCropId(id);
        breedCropDetailDao.deleteAll(details);
    }

    public void deleteDetail(String id) {
        breedCropDetailDao.deleteById(id);
    }
}
